﻿using Furion;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using SignalRHubs.Application.Hubs;
using SignalRHubs.Web.Core.Options;
using System;

namespace SignalRHubs.Web.Core
{
    public class Startup : AppStartup
    {
        public void ConfigureServices(IServiceCollection services)
        {
            //自定义选项
            services.AddConfigurableOptions<CacheOptions>();



            services.AddJwt<JwtHandler>();

            services.AddCorsAccessor();

            services.AddControllers()
                    .AddInjectWithUnifyResult();

            services.AddSignalR( options=>
            {
                options.MaximumReceiveMessageSize = null;   //取消消息长度限制
            });


            var cacheOptions = App.GetOptions<CacheOptions>();
            if (cacheOptions.CacheType == "RedisCache")
            {
                services.AddStackExchangeRedisCache(options =>
                {
                    options.Configuration = cacheOptions.RedisConnectionString; // redis连接配置
                    options.InstanceName = cacheOptions.InstanceName; // 键名前缀
                });
            }
        }

        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.UseHttpsRedirection();

            app.UseRouting();

            app.UseCorsAccessor();

            app.UseAuthentication();
            app.UseAuthorization();

            app.UseInject(string.Empty);

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();

                // 注册集线器
                endpoints.MapHubs();
            });
        }
    }
}